﻿using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Model;
using Dapper_Core.Table;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Dapper_Core.BLL.Impl
{
    public partial class AgentSamAccountBusiness : BaseRepository<AgentSamAccount>
    {
        public bool IsExist(string agentCode, string agentId = "")
        {
            try
            {
                if (string.IsNullOrEmpty(agentCode)) return true;
                Expression<Func<AgentSamAccount, bool>> exp = role => role.AgentCode == agentCode;
                List<AgentSamAccount> list = base.GetList(exp, false).ToList();
                if (list != null && list.Count > 0 && list[0].Id != agentId) return true;
                return false;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "检查代理商编码是否存在失败", "agentCode=" + agentCode + "，agentId=" + agentId, ex, "DapperCore");
                return false;
            }
        }

        public bool ImportData(DataTable table, string type)
        {
            try
            {
                if (table == null || table.Rows.Count < 1)
                {
                    return true;
                }

                int maxCount = 500;
                DateTime now = DateTime.Now;
                List<AgentSamAccount> list = new List<AgentSamAccount>();
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(new AgentSamAccount()
                    {
                        Id = Guid.NewGuid().ToString(),
                        RepresentativeOffice = table.Rows[i]["代表处"].ToString(),
                        Office = table.Rows[i]["办事处"].ToString(),
                        City = table.Rows[i]["地市"].ToString(),
                        SamAccount = table.Rows[i]["域账号"].ToString(),
                        Password = table.Rows[i]["密码"].ToString(),
                        AgentCode = table.Rows[i]["代理商编码"].ToString(),
                        AgentName = table.Rows[i]["代理商名称"].ToString(),
                        Remarks = table.Rows[i]["备注"].ToString(),
                        State = table.Rows[i]["状态"].ToString(),
                        IsDeleted = 0,
                        Creator = SessionUtility.GetBasicUserInfo().PinyinCode,
                        CreateDate = now
                    });
                    if (i != 0 && i % maxCount == 0)
                    {
                        AddList(list);
                        list.Clear();
                    }
                }

                AddList(list);
                list.Clear();
                return true;
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "导入代理商信息失败", "", ex, "DapperCore");
                return false;
            }
        }
    }
}
